package com.pro.util.generator;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class GenerateUtil {
	public static String BASE_PATH = "/com/pro/test/";
	public static String MAPPER_TEMPLATE_NAME = "mybatis_generator_mapper.xml";
	public static String BASE_BEAN = "/com/pro/test/bean/";
	public static String BEAN_PACKAGE = "com.pro.test.bean";
	public static String DAO_PACKAGE = "com.pro.test.dao";
	public static String DAO_DESC_PAHT = "/com/pro/test/dao/";
	public static String DAO_FILE_TEMPLATE_NAME = "${UtableName}Dao.java";
	public static String DAO_TEMPLATE_NAME = "DaoTemplate.tmp";
	public static String BASE_DAO_TEMPLATE_NAME = "BaseDao.tmp";
	public static String BASE_DAO_NAME = "BaseDao.java";
	public static String INTERFACE = "interface";
	public static String TEMPLATE_PATH = "/com/pro/util/generator/";
	public static String MAPPER_DESC_PATH = "/mybatis_mapper/";
	public static String MAPPER_FILE_TEMPALATE_NAME = "mybatis_${tableName}_mapper.xml";
	public static String tableName = "t_polling";//生成表结构
	public static void start(List<Table> tableList) throws Exception{
		for(Table table:tableList){
			if(tableName!=null && table.getTableName().equals(tableName)){
				generateTable(table);
			}
			if(tableName!=null){
				generateTable(table);
			}
		}
	}

	public static void generateTable(Table table) throws Exception {
		table.setDaoPackage(DAO_PACKAGE);
		table.setBeanPackage(BEAN_PACKAGE);
		generateMapper(table);
		generateClassFile(table);
		generateBaseDao(table);
		generateDao(table);
	}

	public static void generateMapper(Table table) throws Exception {
//		table.setPackageName(base);
		String src = getCurrentPath()+TEMPLATE_PATH+MAPPER_TEMPLATE_NAME;
		MapperReader mapperReader = new MapperReader(src,table);
		String value = mapperReader.readFileString();
		String desc = getSourcePath()+MAPPER_DESC_PATH+MAPPER_FILE_TEMPALATE_NAME
				.replaceAll("\\$\\{tableName\\}", table.getTableName().toLowerCase().replace("t_", ""));
		FileUtil.writeToFile(desc, value);
	}

	public static void generateClassFile(Table table) throws Exception {
		ClassBean classBean = new ClassBean();
		classBean.setColumnList(table.getColumnList());
		classBean.setClassName(table.getuTableName());
		classBean.setPackageName("com.pro.test.bean");
		String desc = getCurrentPath()+BASE_BEAN;
		FileUtil.writeToFile(desc+table.getuTableName()+".java", classBean.toString());
	}
	public static void generateDao(Table table) throws Exception{
		String src = getCurrentPath()+TEMPLATE_PATH+DAO_TEMPLATE_NAME;
		MapperReader mapperReader = new MapperReader(src,table);
		mapperReader.setBeanPackage(BEAN_PACKAGE);
		mapperReader.setDaoPackage(DAO_PACKAGE);
		mapperReader.setClassType(INTERFACE);
		String value = mapperReader.readFileString();
		String desc = getCurrentPath()+DAO_DESC_PAHT+DAO_FILE_TEMPLATE_NAME
				.replaceAll("\\$\\{UtableName\\}", table.getuTableName());
		FileUtil.writeToFile(desc, value);
	}
	public static void generateBaseDao(Table table) throws Exception{
		String src = getCurrentPath()+TEMPLATE_PATH+BASE_DAO_TEMPLATE_NAME;
		MapperReader mapperReader = new MapperReader(src,table);
		mapperReader.setBeanPackage(BEAN_PACKAGE);
		String value = mapperReader.readFileString();
		String desc = getCurrentPath()+DAO_DESC_PAHT+BASE_DAO_NAME
				.replaceAll("\\$\\{UtableName\\}", table.getuTableName());
		FileUtil.writeToFile(desc, value);
	}
	public static String getCurrentPath(){
		String path = new File("src").getAbsolutePath();
		String desc = path.replace("\\","/");
		return desc;
	}
	public static String getSourcePath(){
		String path = new File("resource").getAbsolutePath();
		String desc = path.replace("\\","/");
		return desc;
	}
}
